package net.jeeshop.core.pay.wxpay;

import net.jeeshop.core.Overall;
import net.jeeshop.core.pay.IPay;
import net.jeeshop.core.pay.wxpay.util.XMLParse;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.io.SAXReader;
import org.apache.http.conn.ssl.*;

import javax.net.ssl.SSLContext;
import javax.xml.parsers.DocumentBuilderFactory;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
import java.security.KeyStore;

/**
 * Created by xvhuichuang on 2017/7/5.
 */
public class WXPay implements IPay<String, String> {

    private OutputStream outputStream;
    private InputStream inputStream;
    private StringBuffer stringBuffer;
    private BufferedReader bufferedReader;
    private final String UTF8 = "UTF-8";

    @Override
    public String pay(String... t) {
        if (t == null || t[0] == null) return null;
        System.out.println(t[0]);

        return null;
    }

    public static String payHttps(String data,String id) throws Exception {
        //指定读取证书格式为PKCS12
        KeyStore keyStore = KeyStore.getInstance("PKCS12");
        //读取本机存放的PKCS12证书文件
        FileInputStream instream = new FileInputStream(new File("doc/apiclient_cert.p12"));
        try {
            //指定PKCS12的密码, 商户ID
            keyStore.load(instream, id.toCharArray());
        } finally {
            instream.close();
        }
        SSLContext sslcontext = SSLContexts.custom().loadKeyMaterial(keyStore, id.toCharArray()).build();
        //指定TLS版本
        SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(
                sslcontext,new String[] { "TLSv1" },null,
                SSLConnectionSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER);
        //设置httpclient的SSLSocketFactory
        CloseableHttpClient httpclient = HttpClients.custom().setSSLSocketFactory(sslsf).build();
        try {
            /**
             * wx 头信息设置
             */
            //因为牵扯到证书, 所以不使用HttpURLConnection, 改用HttpClient
            HttpPost httpost = new HttpPost("https://api.mch.weixin.qq.com/secapi/pay/refund"); // 设置响应头信息
            httpost.addHeader("Connection", "keep-alive");
            httpost.addHeader("Accept", "*/*");
            httpost.addHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
            httpost.addHeader("Host", "api.mch.weixin.qq.com");
            httpost.addHeader("X-Requested-With", "XMLHttpRequest");
            httpost.addHeader("Cache-Control", "max-age=0");
            httpost.addHeader("User-Agent", "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0) ");
            httpost.setEntity(new StringEntity(data, "UTF-8"));
            CloseableHttpResponse response = httpclient.execute(httpost);
            try {
                HttpEntity entity = response.getEntity();
                String jsonStr = EntityUtils.toString(response.getEntity(), "UTF-8");
                EntityUtils.consume(entity);
                System.out.println("jsonStr" + jsonStr);
                return jsonStr;
            } finally {
                response.close();
            }
        } finally {
            httpclient.close();
        }
    }


    @Override
    public String refund(String... t)  {
        if (t == null || t[0] == null) return null;
        try {
            return payHttps(t[0], t[1]);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
